//
//  BirthdayViewController.m
//  NewChat
//
//  Created by 马欢 on 16/2/28.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "BirthdayViewController.h"
#import "MyDataViewController.h"
#import "MyCenterViewController.h"

@interface BirthdayViewController ()
@property (nonatomic,strong)UIDatePicker *DatePickerV;
@property (nonatomic,strong)UILabel *age;
@property (nonatomic,strong)UILabel *type;
@property (nonatomic,assign)NSInteger year;
@property (nonatomic,assign)NSInteger month;
@property (nonatomic,assign)NSInteger day;
@property (nonatomic,strong)UIImageView *backgroundView;
@property (nonatomic,strong)UILabel *messageLabel;
@end

@implementation BirthdayViewController
//=================一般不改====================
//类方法，单例对象
+(instancetype)sharedMyBirthdayViewController{
    static BirthdayViewController *myBirthdayVC = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        myBirthdayVC = [[BirthdayViewController alloc]init];
    });
    return myBirthdayVC;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addViews];
}
//初始加载视图
-(void)addViews{
    //   背景图片
    self.backgroundView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [self.view addSubview:self.backgroundView];
    [self.view sendSubviewToBack:self.backgroundView];
    
    
    self.edgesForExtendedLayout = UIRectEdgeBottom;
    self.title = @"设置生日";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:self action:@selector(backAction)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:(UIBarButtonItemStylePlain) target:self action:@selector(saveAction)];
    
    UILabel *ageLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 180, 80, 50)];
    ageLabel.text = @"年龄";
    ageLabel.textColor = [UIColor purpleColor];
    ageLabel.textAlignment = NSTextAlignmentCenter;
    ageLabel.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:ageLabel];
    
    UILabel *typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 230, 80, 50)];
    typeLabel.text = @"星座";
    typeLabel.textColor = [UIColor purpleColor];
    typeLabel.textAlignment = NSTextAlignmentCenter;
    typeLabel.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:typeLabel];
    
    self.age = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-170, 180, 80, 50)];
    self.age.textColor = [UIColor purpleColor];
    self.age.textAlignment = NSTextAlignmentCenter;
    self.age.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:self.age];
    
    self.type = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-170, 230, 80, 50)];
    self.type.textColor = [UIColor purpleColor];
    self.type.textAlignment = NSTextAlignmentCenter;
    self.type.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:self.type];
    
    self.messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(95, [UIScreen mainScreen].bounds.size.height-200, 200, 50)];
    self.messageLabel.textAlignment = NSTextAlignmentCenter;
    self.messageLabel.textColor = [UIColor whiteColor];
    self.messageLabel.font = [UIFont systemFontOfSize:20];
    self.messageLabel.numberOfLines = 0;
    [self.view addSubview:self.messageLabel];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, [UIScreen mainScreen].bounds.size.width, 50)];
    label.text = @"*选择出生日期\n系统将会自动转换为年龄及星座";
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:17];
    label.textColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    
    self.DatePickerV = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 65, [UIScreen mainScreen].bounds.size.width, 130)];
    
    self.DatePickerV.maximumDate = [NSDate date];
    
    self.DatePickerV.datePickerMode = UIDatePickerModeDate;
    
    [self.DatePickerV addTarget:self action:@selector(change) forControlEvents:(UIControlEventValueChanged)];
    
    [self.view addSubview:self.DatePickerV];
}
//滑动时间选择器 事件
-(void)change{
    NSDate *select = [self.DatePickerV date];
    
    NSDateFormatter *format = [NSDateFormatter new];
    [format setDateFormat:@"yyyy"];
    NSString *string = [format stringFromDate:select];
    NSString *message = [NSString stringWithFormat:@"%@",string];
    self.age.text = [NSString stringWithFormat:@"%d岁",2016 - [message intValue]];
    self.year = [message intValue];
    
    [format setDateFormat:@"MM"];
    NSString *string2 = [format stringFromDate:select];
    self.month = [string2 intValue];
    [format setDateFormat:@"dd"];
    NSString *string3 = [format stringFromDate:select];
    self.day = [string3 intValue];
    if ((self.month==3 && (self.day>20 && self.day<32))||(self.month == 4 &&(self.day>0 && self.day<20))){
        self.type.text =@"白羊座";
        self.backgroundView.image = [UIImage imageNamed:@"白羊座.jpeg"];
    }else if ((self.month==4 && (self.day>19 && self.day<32))||(self.month == 5 &&(self.day>0 && self.day<21))){
        self.type.text =@"金牛座";
        self.backgroundView.image = [UIImage imageNamed:@"金牛座.jpeg"];
    }else if ((self.month==5 && (self.day>20 && self.day<32))||(self.month == 6 &&(self.day>0 && self.day<22))){
        self.type.text =@"双子座";
        self.backgroundView.image = [UIImage imageNamed:@"双子座.jpeg"];
    }else if ((self.month==6 && (self.day>21 && self.day<32))||(self.month == 7 &&(self.day>0 && self.day<23))){
        self.type.text =@"巨蟹座";
        self.backgroundView.image = [UIImage imageNamed:@"巨蟹座.jpeg"];
    }else if ((self.month==7 && (self.day>22 && self.day<32))||(self.month == 8 &&(self.day>0 && self.day<23))){
        self.type.text =@"狮子座";
        self.backgroundView.image = [UIImage imageNamed:@"狮子座.jpeg"];
    }else if ((self.month==8 && (self.day>22 && self.day<32))||(self.month == 9 &&(self.day>0 && self.day<23))){
        self.type.text =@"处女座";
        self.backgroundView.image = [UIImage imageNamed:@"处女座.jpeg"];
    }else if ((self.month==9 && (self.day>22 && self.day<32))||(self.month == 10 &&(self.day>0 && self.day<24))){
        self.type.text =@"天秤座";
        self.backgroundView.image = [UIImage imageNamed:@"天秤座.jpeg"];
    }else if ((self.month==10 && (self.day>23 && self.day<32))||(self.month == 11 &&(self.day>0 && self.day<23))){
        self.type.text =@"天蝎座";
        self.backgroundView.image = [UIImage imageNamed:@"天蝎座.jpeg"];
    }else if ((self.month==11 && (self.day>22 && self.day<32))||(self.month == 12 &&(self.day>0 && self.day<22))){
        self.type.text =@"射手座";
        self.backgroundView.image = [UIImage imageNamed:@"射手座.jpeg"];
    }else if ((self.month==12 && (self.day>21 && self.day<32))||(self.month == 1 &&(self.day>0 && self.day<20))){
        self.type.text =@"摩羯座";
        self.backgroundView.image = [UIImage imageNamed:@"摩羯座.jpeg"];
    }else if ((self.month==1 && (self.day>19 && self.day<32))||(self.month == 2 &&(self.day>0 && self.day<19))){
        self.type.text =@"水瓶座";
        self.backgroundView.image = [UIImage imageNamed:@"水瓶座.jpeg"];
    }else if ((self.month==2 && (self.day>18 && self.day<32))||(self.month == 3 &&(self.day>0 && self.day<21))){
        self.type.text =@"双鱼座";
        self.backgroundView.image = [UIImage imageNamed:@"双鱼座.jpeg"];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//view将要出现 给背景色
-(void)viewWillAppear:(BOOL)animated{
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    RealReachability *reachability = [RealReachability sharedInstance];
    ReachabilityStatus status = [reachability currentReachabilityStatus];
    if (status == RealStatusNotReachable){
        NSString *imageName = [NSString stringWithFormat:@"%@.jpeg",[user1 objectForKey:@"constellation"]];
        self.backgroundView.image = [UIImage imageNamed:imageName];
        self.type.text = [user1 objectForKey:@"constellation"];
        self.age.text = [user1 objectForKey:@"age"];
    }else{
        NSString *imageName = [NSString stringWithFormat:@"%@.jpeg",[AVUser currentUser][@"constellation"]];
        self.backgroundView.image = [UIImage imageNamed:imageName];
        self.type.text = [AVUser currentUser][@"constellation"];
        self.age.text = [AVUser currentUser][@"age"];
    }
    
    
}
//保存 事件
-(void)saveAction{
        NSDate *select = [self.DatePickerV date];
        NSDateFormatter *format = [NSDateFormatter new];
        [format setDateFormat:@"yyyy年MM月dd日"];
        NSString *string = [format stringFromDate:select];
        NSString *message = [NSString stringWithFormat:@"您选择的日期是%@",string];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"日期" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *A = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

        AVUser *user = [AVUser currentUser];
        NSString *birthday = [NSString stringWithFormat:@"%ld年%02ld月%02ld日",self.year,self.month,self.day];
        [user setObject:birthday forKey:@"birthday"];
        [user setObject:self.type.text forKey:@"constellation"];
        [user setObject:self.age.text forKey:@"age"];
        [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"保存生日成功%@",birthday);
            }
        }];
        //    存入本地
        NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
        //    存入数据
        [user1 setObject:birthday forKey:@"birthday"];
        [user1 setObject:self.type.text forKey:@"constellation"];
        [user1 setObject:self.age.text forKey:@"age"];
        [user1 setObject:@"男" forKey:@"gender"];
        [user1 setObject:@"13121830303" forKey:@"username"];
        //    同步数据
        [user1 synchronize];
        
        [self backAction];
    }];
    
    UIAlertAction *B = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:nil];
    
        [alert addAction:A];
        [alert addAction:B];
    
        [self presentViewController:alert animated:YES completion:nil];
}
//返回 上一层
-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
